{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "a5a3a1a7-97ab-4b7e-970c-a62b8fe1b45e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import wget\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "ebb01d66-a750-4f1b-aba8-65f5ea3e6461",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Downloading: FE\n",
      "100% [............................................................................] 142509 / 142509Downloading: MG\n",
      "100% [..........................................................................] 1329558 / 1329558Downloading: CA\n",
      "100% [............................................................................] 949658 / 949658Downloading: MN\n",
      "100% [............................................................................] 364159 / 364159Downloading: ZN\n",
      "100% [............................................................................] 988449 / 988449Downloading: CO\n",
      "100% [............................................................................] 114983 / 114983Downloading: NI\n",
      "100% [............................................................................] 196016 / 196016"
     ]
    }
   ],
   "source": [
    "## Download the data\n",
    "DSET_URL = {\n",
    "    \"FE\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/FE.txt\",\n",
    "    \"MG\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/MG.txt\",\n",
    "    \"CA\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/CA.txt\",\n",
    "    \"MN\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/MN.txt\",\n",
    "    \"ZN\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/ZN.txt\",\n",
    "    \"CO\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/CO.txt\",\n",
    "    \"NI\": \"https://webs.iiitd.edu.in/raghava/ccpdb/datasets/NI.txt\",\n",
    "}\n",
    "\n",
    "for k, v in DSET_URL.items():\n",
    "    print(f\"Downloading: {k}\") \n",
    "    wget.download(v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "b408f983-e8ca-44a8-b90d-3877696700ed",
   "metadata": {},
   "outputs": [],
   "source": [
    "from Bio import SeqIO\n",
    "\n",
    "def parse_dset(filename: str, write: bool = False) -> pd.DataFrame:\n",
    "    with open(filename) as fasta_file:  # Will close handle cleanly\n",
    "        identifiers = []\n",
    "        chains = []\n",
    "        sequences = []\n",
    "        interactions = []\n",
    "        lengths = []\n",
    "        for seq_record in SeqIO.parse(fasta_file, 'fasta'):  # (generator)\n",
    "            identifiers.append(seq_record.id[:-1])\n",
    "            chains.append(seq_record.id[-1])\n",
    "            lengths.append(len(seq_record.seq)/2)\n",
    "\n",
    "            parsed_sequence_and_interactions = re.split(';', re.sub(\"\\+|-\", lambda match: ';' + match.group(), str(seq_record.seq), count=1), maxsplit=1)#str(seq_record.seq).split(\"-\", maxsplit=1)\n",
    "            sequences.append(parsed_sequence_and_interactions[0])\n",
    "            interactions.append(parsed_sequence_and_interactions[1])\n",
    "\n",
    "    data = zip(identifiers, chains, sequences, interactions,lengths)\n",
    "    df = pd.DataFrame.from_dict(data)\n",
    "    df.columns = [\"PDB\", \"chain\", \"sequence\", \"interacting_residues\", \"length\"]\n",
    "    df[\"interactor\"] = filename.split(\".\")[0]\n",
    "    \n",
    "    if write:\n",
    "        df.to_csv(filename.split(\".\")[0] + \".csv\")\n",
    "    return df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "01c9b42f-70e3-496c-a7f4-5cefbb86376e",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "## Iterate over datasets\n",
    "dataset_list = [parse_dset(k + \".txt\", write=True) for k in DSET_URL.keys()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "23f8b6c4-fb8c-4090-94a1-f4880b1db6cf",
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.concat(dataset_list)\n",
    "df.reset_index(inplace=True, drop=True)\n",
    "df.to_csv(\"PROTEINS_METAL.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "id": "f465b896-8340-45a0-b515-1596f9790b26",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>PDB</th>\n",
       "      <th>chain</th>\n",
       "      <th>sequence</th>\n",
       "      <th>interacting_residues</th>\n",
       "      <th>length</th>\n",
       "      <th>interactor</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1z6o</td>\n",
       "      <td>M</td>\n",
       "      <td>TQCNVNPVQIPKDWITMHRSCRNSMRQQIQMEVGASLQYLAMGAHF...</td>\n",
       "      <td>-------------------------------+--------------...</td>\n",
       "      <td>191.0</td>\n",
       "      <td>FE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1eo2</td>\n",
       "      <td>B</td>\n",
       "      <td>IIWGAYAQRNTEDHPPAYAPGYKTSVLRSPKNALISIAETLSEVTA...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>238.0</td>\n",
       "      <td>FE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1vei</td>\n",
       "      <td>A</td>\n",
       "      <td>MTSFTIPGLSDKKASDVADLLQKQLSTYNDLHLTLKHVHWNVVGPN...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>175.0</td>\n",
       "      <td>FE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>5cry</td>\n",
       "      <td>A</td>\n",
       "      <td>YTRVVWCAVGPEEQKKCQQWSQQSGQNVTCATASTTDDCIVLVLKG...</td>\n",
       "      <td>----------------------------------------------...</td>\n",
       "      <td>348.0</td>\n",
       "      <td>FE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1y67</td>\n",
       "      <td>D</td>\n",
       "      <td>AYTLPQLPYAYDALEPHIDARTMEIHHTKHHQTYVDNANKALEGTE...</td>\n",
       "      <td>-------------------------+--------------------...</td>\n",
       "      <td>214.0</td>\n",
       "      <td>FE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6257</th>\n",
       "      <td>2eaq</td>\n",
       "      <td>A</td>\n",
       "      <td>QFSDMRISINQTPGKSLDFGFTIKWDIPGIFVASVEAGSPAEFSQL...</td>\n",
       "      <td>------------+----------+----------------------...</td>\n",
       "      <td>89.0</td>\n",
       "      <td>NI</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6258</th>\n",
       "      <td>3mgq</td>\n",
       "      <td>F</td>\n",
       "      <td>KRHRKVLRDNIQGITKPAIRRLARRGGVKRISGLIYEETRGVLKVF...</td>\n",
       "      <td>--------+--+----------------------------------...</td>\n",
       "      <td>87.0</td>\n",
       "      <td>NI</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6259</th>\n",
       "      <td>5ht8</td>\n",
       "      <td>A</td>\n",
       "      <td>TKAVTFYEDINYGGAHVHLQPGNYTLSQLNTAKIPNDWMTSLKVPS...</td>\n",
       "      <td>---------------+-+----------------------------...</td>\n",
       "      <td>87.0</td>\n",
       "      <td>NI</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6260</th>\n",
       "      <td>5ht9</td>\n",
       "      <td>A</td>\n",
       "      <td>NAAEVIVYEHVNFGGKSFDATSDQPGAGDNLNDKISSIKVKSGTWR...</td>\n",
       "      <td>---------+------------------------------------...</td>\n",
       "      <td>84.0</td>\n",
       "      <td>NI</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6261</th>\n",
       "      <td>5wwx</td>\n",
       "      <td>A</td>\n",
       "      <td>HHHHHHMSPNLPGQTTVQVRVPYRVVGLVVGPKGATIKRIQQQTHT...</td>\n",
       "      <td>-+-+------------------------------------------...</td>\n",
       "      <td>84.0</td>\n",
       "      <td>NI</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6262 rows × 6 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       PDB chain                                           sequence  \\\n",
       "0     1z6o     M  TQCNVNPVQIPKDWITMHRSCRNSMRQQIQMEVGASLQYLAMGAHF...   \n",
       "1     1eo2     B  IIWGAYAQRNTEDHPPAYAPGYKTSVLRSPKNALISIAETLSEVTA...   \n",
       "2     1vei     A  MTSFTIPGLSDKKASDVADLLQKQLSTYNDLHLTLKHVHWNVVGPN...   \n",
       "3     5cry     A  YTRVVWCAVGPEEQKKCQQWSQQSGQNVTCATASTTDDCIVLVLKG...   \n",
       "4     1y67     D  AYTLPQLPYAYDALEPHIDARTMEIHHTKHHQTYVDNANKALEGTE...   \n",
       "...    ...   ...                                                ...   \n",
       "6257  2eaq     A  QFSDMRISINQTPGKSLDFGFTIKWDIPGIFVASVEAGSPAEFSQL...   \n",
       "6258  3mgq     F  KRHRKVLRDNIQGITKPAIRRLARRGGVKRISGLIYEETRGVLKVF...   \n",
       "6259  5ht8     A  TKAVTFYEDINYGGAHVHLQPGNYTLSQLNTAKIPNDWMTSLKVPS...   \n",
       "6260  5ht9     A  NAAEVIVYEHVNFGGKSFDATSDQPGAGDNLNDKISSIKVKSGTWR...   \n",
       "6261  5wwx     A  HHHHHHMSPNLPGQTTVQVRVPYRVVGLVVGPKGATIKRIQQQTHT...   \n",
       "\n",
       "                                   interacting_residues  length interactor  \n",
       "0     -------------------------------+--------------...   191.0         FE  \n",
       "1     ----------------------------------------------...   238.0         FE  \n",
       "2     ----------------------------------------------...   175.0         FE  \n",
       "3     ----------------------------------------------...   348.0         FE  \n",
       "4     -------------------------+--------------------...   214.0         FE  \n",
       "...                                                 ...     ...        ...  \n",
       "6257  ------------+----------+----------------------...    89.0         NI  \n",
       "6258  --------+--+----------------------------------...    87.0         NI  \n",
       "6259  ---------------+-+----------------------------...    87.0         NI  \n",
       "6260  ---------+------------------------------------...    84.0         NI  \n",
       "6261  -+-+------------------------------------------...    84.0         NI  \n",
       "\n",
       "[6262 rows x 6 columns]"
      ]
     },
     "execution_count": 99,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
